package com.hxht.cmp.service.impl;

import com.hxht.cmp.common.Constants;
import com.hxht.cmp.entity.Court;
import com.hxht.cmp.entity.Warning;
import com.hxht.cmp.mapper.WarningMapper;
import com.hxht.cmp.pojo.SearchCriteria;
import com.hxht.cmp.service.WarnInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 报警信息
 */
@Service
public class WarnInfoServiceImpl implements WarnInfoService {
    @Autowired
    private WarningMapper warningMapper;

    @Override
    public Map<String, Object> selectWarnInfoList(SearchCriteria searchCriteria) {
        //设置分页
        searchCriteria.setPageStart((searchCriteria.getPage() - 1) * searchCriteria.getPageSize());
        int count = 0;
        List<Warning> warings = null;
        if (searchCriteria.getStatus().equals("0")) {
            //单法院
            count = warningMapper.getCountWarnInfo(searchCriteria);
            warings = warningMapper.selectWarnInfo(searchCriteria);
        } else {
            //区域法院
            String uid = Constants.CENTRAL_COURT.getUid();
            String courtUid = searchCriteria.getCourtUid();
            if (!Objects.equals(courtUid, Constants.CENTRAL_COURT.getUid())) {
                List<String> courtUids = new ArrayList<>();
                for (Court court : Constants.COURT_LIST) {
                    if (Objects.equals(courtUid, court.getUid())) {
                        courtUids.add(court.getUid());
                    }
                }
                searchCriteria.setCourtIdList(courtUids);
            }
            count = warningMapper.getCountAllWarnInfo(searchCriteria);
            warings = warningMapper.selectAllWarnInfo(searchCriteria);
        }
        List<Map<String, Object>> waringMapList = new ArrayList<>();
        for (Warning waring : warings) {
            Map<String, Object> map = new HashMap<>();
            Court court = Constants.COURT_MAP.get(waring.getCourtUid());
            map.put("courtName", court.getName());
            //设备名称
            map.put("deviceName", waring.getDeviceName());
            map.put("level", waring.getLevel());
            //报警时间(格式化日期时确保不为null)
            if (null != waring.getTime()) {
                map.put("time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(waring.getTime()));
            } else {
                map.put("time", "");
            }
            map.put("type", waring.getType());
            //报警信息
            map.put("detail", waring.getDetail());
            waringMapList.add(map);
        }
        Map<String,Object> restMap=new HashMap<>();
        restMap.put("count",count);
        restMap.put("waringMapList",waringMapList);
        return restMap;
    }
}